<?php

/**
 * 处理互动记录数据
 */

namespace app\crontab\controller\once;


use app\crontab\model\HouseAuction;
use custom\Log;
use custom\Mongo;
use custom\Redis;
use app\common\lib\RedisKey;
use app\common\lib\RespCode;
use map\BaiDu;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;
use app\crontab\model\House as HouseModel;

class InteractionLog extends Command {


    protected function configure() {
        $this->setName('InteractionLog');
    }

    protected function execute(Input $input, Output $output) {
        $this->initial();
    }

    public function initial() {
        echo "starting\n";
        $data = db('agent_user_interaction_log')->field('agent_user_id')->group('agent_user_id')->select();
        foreach ($data as $key => $value) {
            //名片数据
            $this->dealData1($value['agent_user_id'], 1);
            //名片点赞数据
            $this->dealData1($value['agent_user_id'], 2);
            //贷款预审
            $this->dealData1($value['agent_user_id'], 9);
            //智能选房
            $this->dealData1($value['agent_user_id'], 10);
            //帮填需求
            $this->dealData1($value['agent_user_id'], 12);
            //动态点赞
            $this->dealData2($value['agent_user_id'], 4);
            //查看推荐房源
            $this->dealData2($value['agent_user_id'], 5);
            //关注房源
            $this->dealData2($value['agent_user_id'], 6);
            //房贷测算
            $this->dealData2($value['agent_user_id'], 8);
            //查看房源
            $this->dealData2($value['agent_user_id'], 11);

        }
        echo "end\n";
    }

    /**
     * 1名片查看、9贷款预审、10智能选房、12帮填需求
     * @param int $agent_user_id
     * @param int $type
     */
    public function dealData1($agent_user_id = 0, $type = 0) {
        $last1 = db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type}")->order('itime desc')->find();
        if ($last1) {
            $count1 = db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type}")->count();
            db('agent_user_interaction_log')->where("id={$last1['id']}")->update(['num' => $count1]);
            db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type} and id!={$last1['id']}")->delete();
        }

    }

    /**
     * 4动态点赞、5查看推荐房源、6关注房源、8房贷测算、11查看房源
     * @param int $agent_user_id
     * @param int $type
     */
    public function dealData2($agent_user_id = 0, $type = 0) {
        $data = db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type}")->select();
        $res = [];
        foreach ($data as $key => $value) {
            if ($value['type'] == 4) {
                $index = $value['type'] . '_' . $value['trends_id'];
            } else {
                $index = $value['type'] . '_' . $value['house_id'];
            }

            $res[$index] = $value;
        }
        $res = array_values($res);
        foreach ($res as $key => $value) {
            switch ($value['type']) {
                case 4://动态点赞
                    $this->dealTrendsOrHouse($agent_user_id, 4, $value['trends_id']);
                    break;
                case 5://查看推荐房源
                    $this->dealTrendsOrHouse($agent_user_id, 5, $value['house_id']);
                    break;
                case 6://关注房源
                    $this->dealTrendsOrHouse($agent_user_id, 6, $value['house_id']);
                    break;
                case 8://房贷测算
                    $this->dealTrendsOrHouse($agent_user_id, 8, $value['house_id']);
                    break;
                case 11://查看房源
                    $this->dealTrendsOrHouse($agent_user_id, 11, $value['house_id']);
                    break;
                default:
                    break;

            }
        }
    }

    public function dealTrendsOrHouse($agent_user_id = 0, $type = 0, $trend_or_house_id = 0) {
        if ($type == 4) {
            $file = 'trends_id';
        } else {
            $file = 'house_id';
        }
        $last = db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type} and {$file}={$trend_or_house_id}")->order('itime desc')->find();
        if ($last) {
            $count = db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type} and {$file}={$trend_or_house_id}")->count();
            db('agent_user_interaction_log')->where("id={$last['id']}")->update(['num' => $count]);
            db('agent_user_interaction_log')->where("agent_user_id={$agent_user_id} and type={$type} and {$file}={$trend_or_house_id} and id!={$last['id']}")->delete();
        }


    }
}
